package util.exceptionTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author liw
 * @Date 2021/3/17 13:48
 * @Version 1.0
 */
public class Test {
    public static void main(String[] args) {


        String str = "hello java, i am vary happy! nice to meet you";

        // jdk1.8的写法
        HashMap<Character, Integer> result2 = new HashMap<>(32);
        for (int i = 0; i < str.length(); i++) {
            char curChar = str.charAt(i);

            result2.compute(curChar, (k, v) -> {
                if (v == null) {
                    v = 1;
                } else {
                    v += 1;
                }
                return v;
            });
        }

        // 1 20 300 4000 50000

        List<String> valueStrs = new ArrayList<>();

        valueStrs.add("1");
        valueStrs.add("2");
        valueStrs.add("3");
        valueStrs.add("4");
        valueStrs.add("a");
        valueStrs.add("6");

        // 正确异常都收集
        valueStrs.parallelStream()
                .map(Either.liftWithValue(Integer::parseInt))
                .forEach(value -> {
                    // 异常
                    if (value.getLeft().isPresent()) {
                        System.out.println(value.getLeft().get());
                    }
                    // 正常
                    if (value.getRight().isPresent()) {
                        System.out.println(value.getRight().get());
                    }
                });

        // 过滤异常
        valueStrs.parallelStream()
                .map(Either.liftWithValue(Integer::parseInt)).filter(Either::isRight)
                .forEach(value -> {
                    System.out.println(value.getRight().get());
                });

        System.out.println("--------------------------------");
        valueStrs.parallelStream().map(Either.lift(Integer::parseInt)).filter(Either::isLeft)
                .forEach(value -> {
                    System.out.println(value.getLeft().get());
                });

    }
}
